package com.hula.onion.book.server.spider.platforms.renzhongjinfu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by work on 2018/3/19.
 */
@Component
public class RenZhongJFSpiderHelper {
    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("username", form.getUserName());
        map.put("password", form.getPassword());
        map.put("patchcaCode", "patchcaCode");
        map.put("loginfrom", "pc");
        return map;
    }

    public Map<String, Object> installAccountRequestParams(String token) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("token", token);
        map.put("loginfrom", "pc");
        return map;
    }

    /**
     * 解析账户余额
     *
     * @param json
     * @return
     */
    public CommonResponse parseAccountBalance(String json) {
        try {

            JSONObject accountJson = JSON.parseObject(json);
            if ("000000".equals(accountJson.getString("responseCode"))) {
                AccountInfo accountInfo = new AccountInfo();
                accountInfo.setAmount(accountJson.getJSONObject("obj").getBigDecimal("totalBal"));
                accountInfo.setWaitIntrest(accountJson.getJSONObject("obj").getBigDecimal("waitInterest"));
                return new CommonResponse(accountInfo);
            }
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        } catch (Exception e) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
    }


}
